package com.muyu.danmo.service.impl;


import com.muyu.common.core.domain.Result;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoCollectMaterial;
import com.muyu.danmo.mapper.DanmoCollectMaterialMapper;
import com.muyu.danmo.service.IDanmoCollectMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 拾贝作品材质Service业务层处理
 *
 * @author yangle
 * @date 2024-10-30
 */
@Service
public class DanmoCollectMaterialServiceImpl implements IDanmoCollectMaterialService
{
    @Autowired
    private DanmoCollectMaterialMapper danmoCollectMaterialMapper;

    /**
     * 查询拾贝作品材质
     *
     * @param id 拾贝作品材质主键
     * @return 拾贝作品材质
     */
    @Override
    public DanmoCollectMaterial selectDanmoCollectMaterialById(Long id)
    {
        return danmoCollectMaterialMapper.selectDanmoCollectMaterialById(id);
    }

    /**
     * 查询拾贝作品材质列表
     *
     * @param danmoCollectMaterial 拾贝作品材质
     * @return 拾贝作品材质
     */
    @Override
    public List<DanmoCollectMaterial> selectDanmoCollectMaterialList(DanmoCollectMaterial danmoCollectMaterial)
    {
        return danmoCollectMaterialMapper.selectDanmoCollectMaterialList(danmoCollectMaterial);
    }

    /**
     * 新增拾贝作品材质
     *
     * @param danmoCollectMaterial 拾贝作品材质
     * @return 结果
     */
    @Override
    public int insertDanmoCollectMaterial(DanmoCollectMaterial danmoCollectMaterial)
    {
        return danmoCollectMaterialMapper.insertDanmoCollectMaterial(danmoCollectMaterial);
    }

    /**
     * 修改拾贝作品材质
     *
     * @param danmoCollectMaterial 拾贝作品材质
     * @return 结果
     */
    @Override
    public int updateDanmoCollectMaterial(DanmoCollectMaterial danmoCollectMaterial)
    {
        return danmoCollectMaterialMapper.updateDanmoCollectMaterial(danmoCollectMaterial);
    }

    /**
     * 批量删除拾贝作品材质
     *
     * @param ids 需要删除的拾贝作品材质主键
     * @return 结果
     */
    @Override
    public int deleteDanmoCollectMaterialByIds(Long[] ids)
    {
        return danmoCollectMaterialMapper.deleteDanmoCollectMaterialByIds(ids);
    }

    /**
     * 删除拾贝作品材质信息
     *
     * @param id 拾贝作品材质主键
     * @return 结果
     */
    @Override
    public int deleteDanmoCollectMaterialById(Long id)
    {
        return danmoCollectMaterialMapper.deleteDanmoCollectMaterialById(id);
    }

    @Override
    public Result isCollect(Long id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        DanmoCollectMaterial danmoCollectMaterial1 = danmoCollectMaterialMapper.selectDanmoCollectMaterialByIdAndUserId(id,loginUser.getUserid());
        if (danmoCollectMaterial1 == null){
            danmoCollectMaterial1 = new DanmoCollectMaterial();
            danmoCollectMaterial1.setUserId(loginUser.getUserid());
            danmoCollectMaterial1.setShellfishId(id);
            danmoCollectMaterial1.setMaterialName("true");
            danmoCollectMaterialMapper.insertDanmoCollectMaterial(danmoCollectMaterial1);
            return Result.success(id,"收藏成功");
        }else {
            danmoCollectMaterialMapper.deleteDanmoCollectMaterialById(danmoCollectMaterial1.getId());
            return Result.error("取消收藏成功");
        }
    }

    /**
     * 查询自己收藏
     * @param userid
     * @return
     */
    @Override
    public List<DanmoCollectMaterial> selectUid(Long userid) {
        DanmoCollectMaterial danmoCollectMaterial = new DanmoCollectMaterial();
        danmoCollectMaterial.setUserId(userid);
        return danmoCollectMaterialMapper.selectDanmoCollectMaterialList(danmoCollectMaterial);
    }
}
